package pack;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Transeunte extends Thread {

    private Socket s = null;
    private BufferedReader request = null;
	private DataOutputStream response = null;

    
    public Transeunte(Socket s){
    	setS(s);
    	this.start();
    }
	
    @Override
    public void run(){    	
		try{
			request = new BufferedReader(
					new InputStreamReader(this.s.getInputStream()));
			
			response = new DataOutputStream(this.s.getOutputStream());
			
			
			StringBuilder r = new StringBuilder();
			String tmp;
						
			while( !(tmp = request.readLine()).equals("") ){				
				r.append(tmp + "\n");
				System.out.println(tmp);
			}
			
			response.writeBytes( Interpreter.understand(r) );

			this.s.close();
			
		} catch (IOException e){
			System.err.println(e.getMessage() + "\n" + e.getStackTrace());
		}     
    }

	public Socket getS() {
		return s;
	}

	public void setS(Socket s) {
		this.s = s;
	}

	public BufferedReader getRequest() {
		return request;
	}

	public void setRequest(BufferedReader request) {
		this.request = request;
	}

	public DataOutputStream getResponse() {
		return response;
	}

	public void setResponse(DataOutputStream response) {
		this.response = response;
	}
}
	
